package com.rjpacket.chileme.app;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.rjpacket.chileme.app.fragments.CameraFragment;
import com.rjpacket.chileme.app.fragments.MainFragment;
import com.rjpacket.chileme.app.fragments.NotificationFragment;
import com.rjpacket.chileme.app.fragments.OrderFragment;


public class MainActivity extends BaseActivity implements View.OnClickListener {
    private SlidingMenu slidingMenu;
    private boolean isFirst = true;
    private Context context;
    private int flag;
    private long mCurrentTime;
    private View rHomeHome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        slidingMenu = new SlidingMenu(this,SlidingMenu.SLIDING_WINDOW);

        slidingMenu.setMenu(R.layout.menu_sliding_menu);

        slidingMenu.setMode(SlidingMenu.LEFT);

        slidingMenu.setBehindOffset(120);

        slidingMenu.setShadowDrawable(R.drawable.menu_shadow);

        slidingMenu.setShadowWidth(10);

        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);

        View rHomeLogin = findViewById(R.id.home_menu_login);
        rHomeLogin.setOnClickListener(this);

        rHomeHome = findViewById(R.id.home_menu_home);
        rHomeHome.setOnClickListener(this);

        View rHomeOrder = findViewById(R.id.home_menu_order);
        rHomeOrder.setOnClickListener(this);

        View rHomeHongBao = findViewById(R.id.home_menu_hongbao);
        rHomeHongBao.setOnClickListener(this);

        View rHomeNotification = findViewById(R.id.home_menu_notification);
        rHomeNotification.setOnClickListener(this);

        View rHomeCamera = findViewById(R.id.home_menu_camera);
        rHomeCamera.setOnClickListener(this);

        if(isFirst){
            //TODO 开启线程
            BDLocation location = new BDLocation();
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            Toast.makeText(this,latitude+","+longitude,Toast.LENGTH_LONG).show();
        }

        onClick(rHomeHome);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        switch(id){
            case R.id.home_menu_login:

                break;
            case R.id.home_menu_home:
                MainFragment mainFragment = new MainFragment();
                transaction.replace(R.id.main_container,mainFragment);
                flag = 0;
                transaction.commit();
                break;
            case R.id.home_menu_order:
                OrderFragment orderFragment = new OrderFragment();
                flag = 1;
                transaction.replace(R.id.main_container, orderFragment);
                transaction.commit();
                break;
            case R.id.home_menu_hongbao:
                break;
            case R.id.home_menu_notification:
                NotificationFragment notificationFragment = new NotificationFragment();
                flag = 1;
                transaction.replace(R.id.main_container, notificationFragment);
                transaction.commit();
                break;
            case R.id.home_menu_camera:
                CameraFragment cameraFragment = new CameraFragment();
                flag = 1;
                transaction.replace(R.id.main_container, cameraFragment);
                transaction.commit();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if(flag == 1){
            onClick(rHomeHome);
        }else{
            long timeMillis = System.currentTimeMillis();
            if(timeMillis-mCurrentTime < 2000){
                finish();
            }else{
                Toast.makeText(this,"再次点击退出程序",Toast.LENGTH_SHORT).show();
                mCurrentTime = timeMillis;
            }
        }
    }
}
